// testProplist6Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "testProplist6.h"
#include "testProplist6Dlg.h"
#include <io.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestProplist6Dlg dialog

CTestProplist6Dlg::CTestProplist6Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestProplist6Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestProplist6Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestProplist6Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestProplist6Dlg)
	DDX_Control(pDX, IDC_LIST_TEST, m_listTest);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestProplist6Dlg, CDialog)
	//{{AFX_MSG_MAP(CTestProplist6Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_READ, OnBtnRead)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestProplist6Dlg message handlers

BOOL CTestProplist6Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

    if (m_listTest.GetItemCount() != 0)
    {
        m_listTest.DeleteAllItems();
        ASSERT(m_listTest.GetItemCount() == 0);
    }
    m_listTest.SetExtendedStyle(LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);
    m_listTest.InsertColumn(0, TEXT("Count"));
    m_listTest.InsertColumn(1, TEXT("Ch01"));
	m_listTest.InsertColumn(2, TEXT("Ch02"));
	m_listTest.InsertColumn(3, TEXT("Ch03"));
	m_listTest.InsertColumn(4, TEXT("Ch04"));
	m_listTest.InsertColumn(5, TEXT("Ch05"));
	m_listTest.InsertColumn(6, TEXT("Ch06"));
	m_listTest.InsertColumn(7, TEXT("Ch07"));
	m_listTest.InsertColumn(8, TEXT("Ch08"));

    CRect rc_list_ctrl;
    m_listTest.GetClientRect(rc_list_ctrl);
    m_listTest.SetColumnWidth(0, rc_list_ctrl.Width()/9);
    m_listTest.SetColumnWidth(1, rc_list_ctrl.Width()/9);
	m_listTest.SetColumnWidth(2, rc_list_ctrl.Width()/9);
	m_listTest.SetColumnWidth(3, rc_list_ctrl.Width()/9);
    m_listTest.SetColumnWidth(4, rc_list_ctrl.Width()/9);
	m_listTest.SetColumnWidth(5, rc_list_ctrl.Width()/9);
	m_listTest.SetColumnWidth(6, rc_list_ctrl.Width()/9);
	m_listTest.SetColumnWidth(7, rc_list_ctrl.Width()/9);
	m_listTest.SetColumnWidth(8, rc_list_ctrl.Width()/9);

//     m_listTest.InsertItem(0, "test0");
//     m_listTest.SetItemText(0, 1, "test1");
// 	m_listTest.SetItemText(0, 2, "test2");

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestProplist6Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTestProplist6Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestProplist6Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTestProplist6Dlg::OnBtnRead() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlgOpen(TRUE);
    CString str_file_name;
    if (IDOK == dlgOpen.DoModal())
    {
        str_file_name = dlgOpen.GetPathName();
        FILE* file = fopen((LPSTR)(LPCTSTR)str_file_name, "rb");
        if (NULL != file)
        {
            int size = filelength(fileno(file));
            TRACE("The data file size is %d\r\n", size);
            char* psz_buffer = new char[size];
            if (NULL != psz_buffer)
            {
                TRACE("allocate mem ok!\r\n");
                int i_read_count = fread(psz_buffer, sizeof(char), size, file);

                TRACE("The data read back size is %d\r\n", i_read_count);
                if (i_read_count == size)
                {
                    TRACE("read file ok!\r\n");
                    int i = 0;
                    int i_str_begin = 0, i_str_end = 0, i_line_cnt = 0;
                    char* psz_cur = psz_buffer;
					BOOL b_numeric = FALSE;
                    do 
                    {
                        if ((*psz_cur == '\n') || (i == size))
                        {
							if (0 != vlfOneline.size())
							{
								CString str_item;
								for (int j = 0; j < vlfOneline.size(); j++)
								{
									switch(j)
									{
									case 0:
										viLineNum.push_back(int(vlfOneline[j]));
										str_item.Format("%d", int(vlfOneline[j]));
										m_listTest.InsertItem(i_line_cnt, str_item);
										break;
									case 1:
										vlfCh01.push_back(vlfOneline[j]);
										str_item.Format("%7.5lf", vlfOneline[j]);
										m_listTest.SetItemText(i_line_cnt, 1, str_item);
										break;
									case 2:
										vlfCh02.push_back(vlfOneline[j]);
										str_item.Format("%7.5lf", vlfOneline[j]);
										m_listTest.SetItemText(i_line_cnt, 2, str_item);
										break;
									case 3:
										vlfCh03.push_back(vlfOneline[j]);
										str_item.Format("%7.5lf", vlfOneline[j]);
										m_listTest.SetItemText(i_line_cnt, 3, str_item);
										break;
									case 4:
										vlfCh04.push_back(vlfOneline[j]);
										str_item.Format("%7.5lf", vlfOneline[j]);
										m_listTest.SetItemText(i_line_cnt, 4, str_item);
										break;
									case 5:
										vlfCh05.push_back(vlfOneline[j]);
										str_item.Format("%7.5lf", vlfOneline[j]);
										m_listTest.SetItemText(i_line_cnt, 5, str_item);
										break;
									case 6:
										vlfCh06.push_back(vlfOneline[j]);
										str_item.Format("%7.5lf", vlfOneline[j]);
										m_listTest.SetItemText(i_line_cnt, 6, str_item);
										break;
									case 7:
										vlfCh07.push_back(vlfOneline[j]);
										str_item.Format("%7.5lf", vlfOneline[j]);
										m_listTest.SetItemText(i_line_cnt, 7, str_item);
										break;
									case 8:
										vlfCh08.push_back(vlfOneline[j]);
										str_item.Format("%7.5lf", vlfOneline[j]);
										m_listTest.SetItemText(i_line_cnt, 8, str_item);
										break;
									default:
										break;
									}
								}
								i_line_cnt++;
							}
							vlfOneline.clear();
                        }
						else
						{
							if ((*psz_cur <= '9' && *psz_cur >= '0') || *psz_cur == '.')
							{
								if (!b_numeric)
								{
									i_str_begin = i_str_end = i;
									b_numeric = TRUE;
								}
								else
								{
									i_str_end = i;

								}
							}
							else
							{
								if (b_numeric)
								{
									b_numeric = FALSE;
									char* psz_temp = new char[i_str_end - i_str_begin + 2];
									if (psz_temp)
									{
										memcpy(psz_temp, psz_buffer + i_str_begin, i_str_end - i_str_begin + 1);
										psz_temp[i_str_end - i_str_begin +1] = '\0';
										double temp = atof(psz_temp);
										vlfOneline.push_back(temp);
										delete[] psz_temp;
									}
								}
							}
						}
						
                        i++;
						if (i < size)
						{
							psz_cur++;
						}				
                    } while (i <= size);

                }
                delete[] psz_buffer;
            }
            fclose(file);
            
        }
    }
}

void CTestProplist6Dlg::ClearData()
{
    viLineNum.clear();
    vlfCh01.clear();
    vlfCh02.clear();
    vlfCh03.clear();
    vlfCh04.clear();
    vlfCh05.clear();
    vlfCh06.clear();
    vlfCh07.clear();
    vlfCh08.clear();
    vlfOneline.clear();
}
